package mdl

import ()
import (
	"github.com/opesun/goquery"
	"io/ioutil"
	"strconv"
	//"strings"
	//"strings"
	"github.com/henrylee2cn/surfer"
	"hotime"
	"strings"
	//"fmt"
	"fmt"
)

type Rich struct {
}

func (this *Rich) Lists(time int64, date string, isToday bool) []hotime.Map {
	db := hotime.Db
	var dest = make([]hotime.Map, 0)

	mainUrl := `http://live.365rich.cn/indexall.aspx`
	if isToday {
		resp, err := surfer.Download(&surfer.Request{
			Url:          mainUrl,
			DownloaderID: 1,
		})

		if err != nil {
			fmt.Println(err)
			return dest
		}

		defer resp.Body.Close()
		body, _ := ioutil.ReadAll(resp.Body)
		//fmt.Println(string(body))

		p, _ := goquery.ParseString(string(body))
		trs := p.Find("#table_live")
		trs = trs.Find("tr")
		//fmt.Println(trs.Text())
		//return dest
		for i := 1; i < trs.Length(); i++ {

			ball := hotime.Map{}
			tr := trs.Eq(i)

			if tr.Attr("align") != "center" {
				continue
			}
			ball["bdate"] = date
			ball["type"] = 1
			ball["time"] = time
			ball["id"] = tr.Attr("infoid")
			//ball["order"], _ = strconv.Atoi(tr.Attr("order"))
			ball["status"], _ = strconv.Atoi(tr.Attr("status"))
			ball["gy"] = tr.Attr("gy")
			ball["yy"] = tr.Attr("yy")
			ball["lid"], _ = strconv.Atoi(tr.Attr("lid"))

			ball["sid"], _ = strconv.Atoi(tr.Attr("sid"))

			ball["infoid"], _ = strconv.Atoi(tr.Attr("infoid"))
			ball["r"], _ = strconv.Atoi(tr.Attr("r"))
			ball["living"], _ = strconv.Atoi(tr.Attr("living"))
			tds := tr.Find("td")
			if tds.Length() < 3 {
				continue
			}
			ball["bnumber"] = tds.Eq(0).Text()
			ball["bgame"] = tds.Eq(1).Text()
			ball["blunci"] = ""
			ball["btime"] = tds.Eq(2).Text()
			ball["bstate"] = tds.Eq(3).Text()

			ball["bmasterteam"] = tds.Eq(4).Text()
			//a := tds.Eq(5)
			//a = a.Find("a")
			//ball["bmasterhref"] = a.Attr("href")
			as := tds.Eq(6)
			as = as.Find("a")
			ball["bpoints"] = tds.Eq(5).Text()
			ball["bslaveteam"] = tds.Eq(6).Text()
			//a = tds.Eq(7)
			//a = a.Find("a")
			//ball["bslavehref"] = a.Attr("href")
			ball["bhalf"] = tds.Eq(7).Text()
			//if isToday {
			//	ball["zhishul"] = tds.Eq(9).Text()
			//	ball["bet365"] = tds.Eq(10).Text()
			//	ball["zhishur"] = tds.Eq(11).Text()
			//
			//	ball["bmoney"] = ""
			//} else {
			//	ball["bmoney"] = tds.Eq(9).Html()
			//
			//ball["bmoney"] = ""


			bifen:=tds.Eq(5).Text()
			if !strings.Contains(bifen,"-"){
				ball["bmoney"] = "3"
			}else if len(bifen)==1{
				ball["bmoney"] = "3"
			}else{
				gekai:=strings.Index(bifen,"-")

				z,_:=hotime.ObjToInt(hotime.Substr(bifen,0,gekai))
				k,_:=hotime.ObjToInt(hotime.Substr(bifen,gekai+1,len(bifen)-gekai-1))
				if z>k{
					ball["bmoney"]="1"//胜
				}
				if z==k{
					ball["bmoney"]="0"//平
				}
				if z<k{
					ball["bmoney"]="2"//输
				}


			}







			ball["zhishul"] = ""
			ball["bet365"] = ""
			ball["zhishur"] = ""
			//}
			//
			//videoNum := 10
			//if isToday {
			//	videoNum = 12
			//}
			//a = tds.Eq(videoNum)
			//a = a.Find("a")
			//ball["bvideo"] = a.Attr("href")
			//ball["bvideo"] = strings.Replace(ball["bvideo"].(string), "./", "http://live.500.com/", -1)
			as = tds.Eq(12)
			as = as.Find("a")
			str := as.Eq(0).Attr("onclick")
			ball["bfenxi"] = "http://zq.win007.com/analysis/" + strings.Replace(strings.Replace(str, "analysis(", "", -1), ")", "", -1) + "cn.htm"
			str = as.Eq(1).Attr("onclick")
			ball["bya"] = strings.Replace(strings.Replace(str, "AsianOdds(", "", -1), ");", "", -1)
			ball["fid"] = ball["bya"]
			str = as.Eq(2).Attr("onclick")
			ball["bou"] = "http://op1.win007.com/oddslist/" + strings.Replace(strings.Replace(str, "EuropeOdds(", "", -1), ")", "", -1) + ".htm"

			//if !isToday {
			//ball["id"]=db.Insert("ball",ball);
			//}
			dest = append(dest, ball)
		}

		return dest

	}

	//从数据库获取
	dest = db.Select("ball", "*", hotime.Map{"AND": hotime.Map{"bdate": date, "type": "1"}})
	if dest != nil && len(dest) != 0 {
		return dest
	}

	url := "http://bf.win007.com/football/hg/Over_" + strings.Replace(date, "-", "", -1) + ".htm"

	resp, err := surfer.Download(&surfer.Request{
		Url:          url,
		DownloaderID: 1,
	})
	//client := http.Client{}
	//res, _ := client.Get(url)
	//defer res.Body.Close()

	if err != nil {
		fmt.Println(err)
		return dest
	}
	//b, err = ioutil.ReadAll(resp.Body)
	//defer surfer.DestroyJsFiles()
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Println(string(body))

	p, _ := goquery.ParseString(string(body))
	trs := p.Find("#table_live")
	trs = trs.Find("tr")
	//fmt.Println(trs.Text())
	//return dest
	for i := 1; i < trs.Length(); i++ {
		ball := hotime.Map{}
		tr := trs.Eq(i)
		ball["bdate"] = date
		ball["type"] = 1
		ball["time"] = time
		ball["id"] = tr.Attr("infoid")
		//ball["order"], _ = strconv.Atoi(tr.Attr("order"))
		ball["status"], _ = strconv.Atoi(tr.Attr("status"))
		ball["gy"] = tr.Attr("gy")
		ball["yy"] = tr.Attr("yy")
		ball["lid"], _ = strconv.Atoi(tr.Attr("lid"))

		ball["sid"], _ = strconv.Atoi(tr.Attr("sid"))

		ball["infoid"], _ = strconv.Atoi(tr.Attr("infoid"))
		ball["r"], _ = strconv.Atoi(tr.Attr("r"))
		ball["living"], _ = strconv.Atoi(tr.Attr("living"))
		tds := tr.Find("td")
		if tds.Length() < 3 {
			continue
		}
		ball["bnumber"] = tds.Eq(0).Text()
		ball["bgame"] = tds.Eq(0).Text()
		ball["blunci"] = ""
		ball["btime"] = tds.Eq(1).Text()
		ball["bstate"] = tds.Eq(2).Text()

		ball["bmasterteam"] = tds.Eq(3).Text()
		//a := tds.Eq(5)
		//a = a.Find("a")
		//ball["bmasterhref"] = a.Attr("href")
		as := tds.Eq(6)
		as = as.Find("a")
		ball["bpoints"] = tds.Eq(4).Text()
		ball["bslaveteam"] = tds.Eq(5).Text()
		//a = tds.Eq(7)
		//a = a.Find("a")
		//ball["bslavehref"] = a.Attr("href")
		ball["bhalf"] = tds.Eq(6).Text()
		//if isToday {
		//	ball["zhishul"] = tds.Eq(9).Text()
		//	ball["bet365"] = tds.Eq(10).Text()
		//	ball["zhishur"] = tds.Eq(11).Text()
		//
		//	ball["bmoney"] = ""
		//} else {
		//	ball["bmoney"] = tds.Eq(9).Html()
		//
		bifen:=tds.Eq(4).Text()
		if !strings.Contains(bifen,"-"){
			ball["bmoney"] = "3"
		}else if len(bifen)==1{
			ball["bmoney"] = "3"
		}else{
			gekai:=strings.Index(bifen,"-")

			z,_:=hotime.ObjToInt(hotime.Substr(bifen,0,gekai))
			k,_:=hotime.ObjToInt(hotime.Substr(bifen,gekai+1,len(bifen)-gekai-1))
			if z>k{
				ball["bmoney"]="1"//胜
			}
			if z==k{
				ball["bmoney"]="0"//平
			}
			if z<k{
				ball["bmoney"]="2"//输
			}


		}



		ball["zhishul"] = ""
		ball["bet365"] = ""
		ball["zhishur"] = ""
		//}
		//
		//videoNum := 10
		//if isToday {
		//	videoNum = 12
		//}
		//a = tds.Eq(videoNum)
		//a = a.Find("a")
		//ball["bvideo"] = a.Attr("href")
		//ball["bvideo"] = strings.Replace(ball["bvideo"].(string), "./", "http://live.500.com/", -1)
		as = tds.Eq(9)
		as = as.Find("a")
		str := as.Eq(0).Attr("onclick")
		ball["bfenxi"] = "http://zq.win007.com/analysis/" + strings.Replace(strings.Replace(str, "analysis(", "", -1), ")", "", -1) + "cn.htm"
		str = as.Eq(1).Attr("onclick")
		ball["bya"] = strings.Replace(strings.Replace(str, "AsianOdds(", "", -1), ")", "", -1)
		ball["fid"] = ball["bya"]
		str = as.Eq(2).Attr("onclick")
		ball["bou"] = "http://op1.win007.com/oddslist/" + strings.Replace(strings.Replace(str, "EuropeOdds(", "", -1), ")", "", -1) + ".htm"

		//if !isToday {
		ball["id"] = db.Insert("ball", ball)
		//}
		dest = append(dest, ball)
	}
	return dest
}

func (this *Rich) Info(fid string) []hotime.Map {
	db := hotime.Db
	data := make([]hotime.Map, 0)
	get := db.Get("ball", "*", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "1"}})
	if get != nil {
		data = db.Select("yazhi", "*", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "1"}})
		if data != nil && len(data) != 0 {

			return data
		}
	}

	var url = "http://zq.win007.com/analysis/" + fid + "cn.htm"
	resp, err := surfer.Download(&surfer.Request{
		Url:          url,
		DownloaderID: 1,
	})
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	p, err := goquery.ParseString(string(body))
	if err != nil {
		panic(err)
		return nil
	}
	//db, _ := sql.Open("sqlite3", "data.db")
	//defer db.Close()
	db.Delete("yazhi", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "1"}})

	trs := p.Find("#porlet_1")
	trs = trs.Find("tr")
	for i := 2; i < trs.Length(); i++ {
		tds := trs.Eq(i)
		//tables := tds.Find(".pl_table_data")
		tds = tds.Find("td")

		if tds.Eq(2).Text() == "" || tds.Eq(1).Text() == "滚球" || tds.Eq(1).Text() != "初盘" {
			continue
		}

		//		fmt.Println(trs.Eq(i).Html())
		yazhi := hotime.Map{"fid": fid, "type": 1}

		yazhi["quancheng"] = tds.Eq(0).Text()
		if yazhi["quancheng"] == "" && len(data) > 0 {
			yazhi["quancheng"] = data[len(data)-1]["quancheng"]
		}
		//a := tds.Eq(1)
		//a = a.Find("a")
		yazhi["quanchenghref"] = ""
		//table0 := tables.Eq(0)
		//ttds := table0.Find("td")

		yazhi["time"] = ""

		//table1 := tables.Eq(1)
		//ttds = table1.Find("td")
		yazhi["cping"] = tds.Eq(9).Text()

		cpan:=tds.Eq(10).Text()
		cpan=strings.Replace(cpan,"*","受让",-1)
		cpan=strings.Replace(cpan,"平/半","平手/半球",-1)
		cpan=strings.Replace(cpan,"半/一","半球/一球",-1)
		cpan=strings.Replace(cpan,"球半/两","球半/两球",-1)
		cpan=strings.Replace(cpan,"一/球半","一球/球半",-1)
		yazhi["cpan"]=cpan;

		yazhi["cying"] = tds.Eq(11).Text()

		//		fmt.Println(tds.Eq(12).Html())
		yazhi["ctime"] = ""
		a := tds.Eq(12)
		a = a.Find("a")
		yazhi["masterhref"] = "http://vip.win007.com/" + a.Eq(0).Attr("href")
		yazhi["slavehref"] = "http://vip.win007.com/" + a.Eq(1).Attr("href")
		yazhi["synchref"] = "http://vip.win007.com/" + a.Eq(2).Attr("href")

		i += 1
		tds = trs.Eq(i)
		//tables := tds.Find(".pl_table_data")
		tds = tds.Find("td")
		yazhi["ping"] = tds.Eq(8).Text()
		//yazhi["pan"] = tds.Eq(9).Text()

		pan:=tds.Eq(9).Text()
		pan=strings.Replace(pan,"*","受让",-1)
		pan=strings.Replace(pan,"平/半","平手/半球",-1)
		pan=strings.Replace(pan,"半/一","半球/一球",-1)
		pan=strings.Replace(pan,"球半/两","球半/两球",-1)
		pan=strings.Replace(pan,"一/球半","一球/球半",-1)
		yazhi["pan"]=pan;




		yazhi["ying"] = tds.Eq(10).Text()

		//sql := "insert  into `yazhi`(`fid`,`quancheng`,`quanchenghref`,`ping`,`pan`,`ying`,`time`,`cping`,`cpan`,`cying`,`ctime`,`masterhref`,`slavehref`,`synchref`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
		//db.Exec(sql, yazhi.Fid, yazhi.Quancheng, yazhi.QuanchengHref, yazhi.Ping,
		//	yazhi.Pan, yazhi.Ying, yazhi.Time, yazhi.Cping, yazhi.Cpan, yazhi.Cying, yazhi.Ctime, yazhi.Masterhref, yazhi.Slavehref, yazhi.Synchref)

		yazhi=(&Yuche{}).Get(yazhi)
		db.Insert("yazhi", yazhi)
		data = append(data, yazhi)
	}
	return data
}
